package com.jt.intercector;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**
 * springmvc 中的拦截对象
 * 此对象在执行controller方法之前执行
 */
public class TimeInterceptor implements HandlerInterceptor {
    /**
     * 此方法时默认方法
     * 此对象在执行controller方法之前执行
     * @param request
     * @param response
     * @param handler
     * @return  true 放行
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("===preHandler===");
        LocalTime now = LocalTime.now();//获取当前时间
        int hour = now.getHour();
        if(hour<8 || hour>=21){
            throw new RuntimeException("请在规定时间访问。。。。");
        }
        return true;
    }
}
